package com.tn.rtdata;

import java.util.Collection;

import org.easymock.IAnswer;

import static org.easymock.EasyMock.getCurrentArguments;
import static org.junit.Assert.assertTrue;

/**
 * An implementation of <code>IAnswer</code> that checks the contents of a <code>Collection</code> argument contains the expected items.
 */
public class CollectionCheckingAnswer implements IAnswer<Object>
{
  private Collection expectedCollection;
  private int argumentIndex;

  /**
   * Creates a new <code>CollectionCheckingAnswer</code> initialized with the <code>expectedCollection</code>.
   */
  public CollectionCheckingAnswer(Collection<?> expectedCollection, int argumentIndex)
  {
    this.expectedCollection = expectedCollection;
    this.argumentIndex = argumentIndex;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Object answer() throws Throwable
  {
    assertTrue(
      "Collection contents do not match those that were expected.",
      ((Collection)getCurrentArguments()[argumentIndex]).containsAll(expectedCollection)
    );
    return null;
  }
}
